package com.hh.flex.listener;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.hh.common.core.domain.BaseEntity;
import com.hh.common.exception.ServiceException;
import com.hh.security.utils.SecurityUtils;
import com.mybatisflex.annotation.UpdateListener;

import java.util.Date;

/**
 * @description:  全局修改监听器
 * @author minliuhua
 * @date: 2023/5/2 22:53
 */
public class HhUpdateListener implements UpdateListener {

    @Override
    public void onUpdate(Object object) {
        try {
            if (ObjectUtil.isNotNull(object)) {
                BaseEntity entity = (BaseEntity) object;
                entity.setUpdateBy(SecurityUtils.getUsernameNe());
                entity.setUpdateTime(new Date());
            }
        } catch (Exception e) {
            throw new ServiceException("修改自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
        }
    }
}
